package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.Service.SetmealService;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.dto.DishDto;

import com.itheima.dto.SetmealDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetmealController {


    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto) {

        int row = setmealService.add(setmealDto);

        if (row > 0) {

            return R.success("添加套餐成功");

        } else if (row == 0) {

            return R.success("添加套餐失败");
        }
        return R.error("套餐名重复");
    }


    @GetMapping("/page")
    public R findPage(PageParam pageParam) {


        IPage<SetmealDto> page = setmealService.findPage(pageParam);


        return R.success(page);


    }

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        int row = setmealService.delete(ids);

        if (row > 0) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 销售状态批量更改
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status, Long[] ids) {
        boolean b = setmealService.updateStatus(status, ids);
        if (b) {
            return R.success("状态修改成功");
        }
        return R.error("状态更新失败");
    }

    @GetMapping("/list")
    public R findList(Long categoryId,int status){
/*        System.out.println("categoryId = " + categoryId);
        System.out.println("status = " + status);*/
        List<Setmeal> list = setmealService.findList(categoryId, status);
        return R.success(list);
/*        if (list!=null){
            return R.success(list);
        }
        return R.error("查询失败");*/
    }
}
